package com.shuorikeji.huohuo.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.shuorikeji.huohuo.R;
import com.shuorikeji.huohuo.adapter.MhRankAdapter;
import com.shuorikeji.huohuo.adapter.MhTypeAdapter;
import com.shuorikeji.huohuo.base.BasePresenterActivity;
import com.shuorikeji.huohuo.bean.Mh1FlMainBean;
import com.shuorikeji.huohuo.bean.Mh1FlSubBean;
import com.shuorikeji.huohuo.bean.MhInfoBean;
import com.shuorikeji.huohuo.bean.MhInfoMainBean;
import com.shuorikeji.huohuo.callback.ClickCallBack;
import com.shuorikeji.huohuo.common.Constant;
import com.shuorikeji.huohuo.mvp.AllPresenter;
import com.shuorikeji.huohuo.ui.mh.MhDetailActivity;
import com.shuorikeji.huohuo.utils.BusinessUtils;
import com.shuorikeji.huohuo.utils.SpacesItemDecoration;
import com.shuorikeji.huohuo.utils.ToastShowUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class MhRankActivity extends BasePresenterActivity<AllPresenter> {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_top_bg)
    ImageView ivTopBg;
    @BindView(R.id.rv_type)
    RecyclerView rvType;
    @BindView(R.id.srl_layout)
    SmartRefreshLayout srlView;
    @BindView(R.id.rv_mh)
    RecyclerView rvMh;

    private List<Mh1FlSubBean> mh1FlList = new ArrayList<>();//漫画一级分类数据
    private MhTypeAdapter mhTypeAdapter;
    private int mh1FlId;//漫画一级分类id
    private int curPage;
    private List<MhInfoBean> mhList = new ArrayList<>();
    private MhRankAdapter mhRankAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getmImmersionBar().reset().statusBarColor(R.color.transparent).statusBarDarkFont(true, 0.2f).init();
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_mh_rank);
    }

    @Override
    protected void initData() {
        initSmartRefreshLayout();
        initAdapter();
        getMh1Fl();
        ivBack.setOnClickListener(view -> {
            finish();
        });
    }

    private void initSmartRefreshLayout() {
        srlView.setRefreshHeader(new ClassicsHeader(MhRankActivity.this));
        srlView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                //刷新
                curPage = 1;
                getAllMh();
            }
        });
        srlView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                //加载更多
                curPage = curPage + 1;
                getAllMh();
            }
        });
    }

    private void initAdapter() {
        rvType.setLayoutManager(new LinearLayoutManager(MhRankActivity.this, LinearLayoutManager.VERTICAL, false));
        mhTypeAdapter = new MhTypeAdapter(MhRankActivity.this, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        Mh1FlSubBean data = (Mh1FlSubBean) object;
                        for (int i = 0; i < mh1FlList.size(); i++) {
                            if (data.getId() == mh1FlList.get(i).getId()) {
                                mh1FlList.get(i).setSec(true);
                            } else {
                                mh1FlList.get(i).setSec(false);
                            }
                        }
                        mhTypeAdapter.setData(mh1FlList);
                        mh1FlId = data.getId();
                        tvTitle.setText(data.getName());
                        curPage = 1;
                        getAllMh();
                        break;
                }
            }
        });
        rvType.setAdapter(mhTypeAdapter);

        rvMh.setLayoutManager(new LinearLayoutManager(MhRankActivity.this, LinearLayoutManager.VERTICAL, false));
        rvMh.addItemDecoration(new SpacesItemDecoration(0, 0, 15, 0));
        mhRankAdapter = new MhRankAdapter(MhRankActivity.this, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        MhInfoBean data = (MhInfoBean) object;
                        Intent intent = new Intent(MhRankActivity.this, MhDetailActivity.class);
                        intent.putExtra("MH_ID", data.getId());
                        startActivity(intent);
                        break;
                }
            }
        });
        rvMh.setAdapter(mhRankAdapter);
    }

    private void getMh1Fl() {
        mPresenter.getMh1Fl(false, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        Mh1FlMainBean data = (Mh1FlMainBean) object;
                        mh1FlList.clear();
                        mh1FlList.addAll(data.getData());
                        if (mh1FlList != null && mh1FlList.size() > 0) {
                            for (int i = 0; i < mh1FlList.size(); i++) {
                                mh1FlList.get(i).setSec((i == 0) ? true : false);
                            }
                            tvTitle.setText(mh1FlList.get(0).getName());
                            mh1FlId = mh1FlList.get(0).getId();
                            mhTypeAdapter.setData(mh1FlList);
                            curPage = 1;
                            getAllMh();
                        }
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(MhRankActivity.this, msg);
                        break;
                }
            }
        });
    }

    /**
     * 获取所有漫画
     */
    private void getAllMh() {
        mPresenter.getAllMh(1, -1, 20, curPage, "", "", mh1FlId, -1, -1, -1,
                -1, -1, -1, -1, -1, -1, true, new ClickCallBack() {
                    @Override
                    public void onClick(int flag, Object object) {
                        srlView.finishRefresh();
                        srlView.finishLoadMore();
                        switch (flag) {
                            case 1:
                                MhInfoMainBean data = (MhInfoMainBean) object;
                                if (curPage == 1) {
                                    mhList.clear();
                                }
                                mhList.addAll(data.getData().getData());
                                if (mhList != null && mhList.size() > 0) {
                                    BusinessUtils.setLoadImg(MhRankActivity.this, Constant.IMG_OSS_BASE_URL + mhList.get(0).getCover_image(), ivTopBg);
                                }
                                mhRankAdapter.setData(mhList);
                                break;
                            case 2:
                                String msg = (String) object;
                                ToastShowUtil.showToastCenter(MhRankActivity.this, msg);
                                break;
                        }
                    }
                });
    }
}